$(document).ready(function(){

});

function ownerChoose(value)
{
    if(value === 1)
    {
        $("#owner_legal_entity_wrapper").addClass('hide');
        $("#owner_citizen_wrapper").removeClass('hide');
    }else if(value === 2)
    {
        $("#owner_citizen_wrapper").addClass('hide');
        $("#owner_legal_entity_wrapper").removeClass('hide');

    }else{
        alert('Та аль нэгийг нь сонгоно уу');
    }
}

function addOwnerPopup()
{
    // remove checked
    $("#owner_popup_citizen").removeAttr('checked');
    $("#owner_popup_citizen").closest('span').removeClass('checked');
    $("#owner_popup_legal_entity").removeAttr('checked');
    $("#owner_popup_legal_entity").closest('span').removeClass('checked');
    
    // close contents
    $("#owner_citizen_wrapper").addClass('hide');
    $("#owner_legal_entity_wrapper").addClass('hide');

    // set default to empty
    $("#owner_popup_family_name").val('');
    $("#owner_popup_last_name").val('');
    $("#owner_popup_first_name").val('');
    $("#owner_popup_register_number").val('');
    $("#owner_popup_address_1").val('');
    $("#owner_popup_address_2").val('');
    $("#owner_popup_address_full").val('');
    $("#owner_popup_phone").val('');
    $("#owner_popup_email").val('');
    
    // set default to empty
    $("#owner_popup_lentity_name").val('');
    $("#owner_popup_lentity_register_number").val('');
    $("#owner_popup_lentity_address_1").val('');
    $("#owner_popup_lentity_address_2").val('');
    $("#owner_popup_lentity_address_full").val('');
    $("#owner_popup_lentity_phone").val('');
    $("#owner_popup_lentity_email").val('');
    
}

function addOwner()
{
    var hiddenOwnerCount = parseInt($("#hiddenAssetOwnerCount").val());
    hiddenOwnerCount++;
    if($('#owner_popup_citizen').is(':checked'))
    {
        var owner_popup_family_name = $("#owner_popup_family_name").val();
        var owner_popup_last_name = $("#owner_popup_last_name").val();
        var owner_popup_first_name = $("#owner_popup_first_name").val();
        var owner_popup_register_number = $("#owner_popup_register_number").val();
        var owner_popup_address_1 = $("#owner_popup_address_1").val();
        var owner_popup_address_2 = $("#owner_popup_address_2").val();
        var owner_popup_address_full = $("#owner_popup_address_full").val();
        var owner_popup_phone = $("#owner_popup_phone").val();
        var owner_popup_email = $("#owner_popup_email").val();
        
        var ownerList = "<tr class=\"ownerListRow\"><td>"+hiddenOwnerCount+"</td><td>"+owner_popup_first_name+"</td><td>"+owner_popup_register_number+"</td><td>"+owner_popup_phone+"</td></tr>";
        
        if(hiddenOwnerCount === 1)
        {
            $("#assetOwnerTbody").append(ownerList);
        }else{
            $(".ownerListRow").last().after(ownerList);
        }
        
    }else if($('#owner_popup_legal_entity').is(':checked'))
    {
        var owner_popup_lentity_name = $("#owner_popup_lentity_name").val();
        var owner_popup_lentity_register_number = $("#owner_popup_lentity_register_number").val();
        var owner_popup_lentity_address_1 = $("#owner_popup_lentity_address_1").val();
        var owner_popup_lentity_address_2 = $("#owner_popup_lentity_address_2").val();
        var owner_popup_lentity_address_full = $("#owner_popup_lentity_address_full").val();
        var owner_popup_lentity_phone = $("#owner_popup_lentity_phone").val();
        var owner_popup_lentity_email = $("#owner_popup_lentity_email").val();
        
        var ownerList = "<tr class=\"ownerListRow\"><td>"+hiddenOwnerCount+"</td><td>"+owner_popup_lentity_name+"</td><td>"+owner_popup_lentity_register_number+"</td><td>"+owner_popup_lentity_phone+"</td></tr>";   
        
        if(hiddenOwnerCount === 1)
        {
            $("#assetOwnerTbody").append(ownerList);
        }else{
            $(".ownerListRow").last().after(ownerList);
        }
    }
    $("#hiddenAssetOwnerCount").val(hiddenOwnerCount);
    $('#ownerAddPopup').modal('hide');
    
}